package interaction.crash;

import entity.*;
import entity.props.Bomb;
import entity.props.BulletBuff;
import entity.props.HealthPack;
import interaction.end.EndLogic;

import java.util.ArrayList;

/**
 * @Author:zhengbowen
 * @Date: 2025/09/09 14:53
 * @Description:处理各个实体之间的碰撞逻辑
 */
public class EntityCrash {
    public static int score = 0;
    public static ArrayList<BulletBuff> bulletBuffss = new ArrayList<>();

    //子弹与飞机的碰撞逻辑
    //判断球是否被击中,如果被击中了还要做相应的处理
    public static void isShootBall(Plane plane) {
        //遍历所有的球
        for (int i = Ball.balls.size() - 1; i >= 0; i--) {
            Ball ball = Ball.balls.get(i);
            //判断当前遍历到的球和每颗子弹是否碰撞
            ArrayList<Bullet> Bullets = plane.getBullets();
            for (int j = Bullets.size() - 1; j >= 0; j--) {
                Bullet bullet = Bullets.get(j);
                //shootBy 可以判断是否和子弹碰撞了，如果碰撞了，血量减少
                if (ball.shootBy(bullet)) {
                    //如果碰撞了就做相应的处理
                    //小球血量减少到一半会分裂
                    if (!ball.getSpitFlag() && ball.getCurrentBlood() < ball.getBlood() / 2 && ball.getCurrentBlood() > 2) {
                        ball.split();
                        //只执行一次
                        ball.setSpitFlag(true);
                    }
                    //飞机获得经验
                    plane.eatExp(1);
                    //子弹消失
                    Bullets.remove(bullet);
                    // 小球消失, 得分加1
                    score++;
                    //判断血量是否为0 ，如果为0，球也消失
                    if (ball.getCurrentBlood() <= 0) {
                        Ball.balls.remove(ball);
                        // 增加大招能量
                        plane.addSuperSkillEnergy(4);
                    }

                }
            }
        }
    }

    //判断飞机与球的碰撞逻辑
    public static void isCrash(Plane plane) {
        boolean flag = false;
        for (int i = Ball.balls.size() - 1; i >= 0; i--) {
            Ball ball = Ball.balls.get(i);
            flag = ball.getY() < plane.getY() + plane.getHeight() && ball.getY() + ball.getHeight() > plane.getY()
                    && ball.getX() + ball.getWidth() > plane.getX() && ball.getX() < plane.getX() + plane.getWidth();
            if (flag && plane.getCurrentBlood() <= 1) {
                //说明撞上  让球消失，
                Ball.balls.remove(ball);
                //飞机血量减少
                plane.setCurrentBlood(plane.getCurrentBlood() - 1);
                //结束逻辑
                EndLogic.changePlaneImag(plane);
                //设置为游戏结束
                EndLogic.gameOver = true;
                break;
            } else if (flag && plane.getCurrentBlood() != 1) {
                //说明撞上  让球消失，
                Ball.balls.remove(ball);
                plane.setCurrentBlood(plane.getCurrentBlood() - 1);
            }
        }
    }

    // 判断飞机与血包的碰撞逻辑
    public static void isCrashHealthPack(Plane plane) {
        boolean flag = false;
        for (int i = HealthPack.healthPacks.size() - 1; i >= 0; i--) {
            HealthPack healthPack = HealthPack.healthPacks.get(i);
            flag = healthPack.getY() < plane.getY() + plane.getHeight() && healthPack.getY() + healthPack.getHeight() > plane.getY()
                    && healthPack.getX() + healthPack.getWidth() > plane.getX() && healthPack.getX() < plane.getX() + plane.getWidth();
            if (flag) {
                //说明撞上  让血包消失，
                HealthPack.healthPacks.remove(healthPack);
                // 飞机生命值加1
                if (plane.getCurrentBlood() < 10) {
                    plane.setCurrentBlood(plane.getCurrentBlood() + healthPack.healing);
                }
                System.out.println("飞机血量:" + plane.getCurrentBlood());
            }
        }
    }

    //判断子弹与Boss的碰撞逻辑
    public static void isShootBoss(Boss boss, Plane plane) {
        if (boss != null) {
            ArrayList<Bullet> Bullets = plane.getBullets();
            for (int j = Bullets.size() - 1; j >= 0; j--) {
                Bullet bullet = Bullets.get(j);
                if (boss.getCurrentBlood() < 1) {
                    boss.setCurrentBlood(0);
                    Boss.bossLiveStatus = false;
                    break;
                }
                //子弹打中boss
                else if (boss.hitBy(bullet)) {
                    boss.setCurrentBlood(boss.getCurrentBlood() - bullet.getDamage());
                }
            }
        }
    }

    //判断boss子弹与飞机的碰撞逻辑
    public static void isShootPlane(Boss boss, Plane plane) {
        if (boss != null) {
            boolean flag = false;
            for (int i = boss.bossBullets.size() - 1; i >= 0; i--) {
                Bullet bullet = boss.bossBullets.get(i);
                flag = bullet.getY() < plane.getY() + plane.getHeight() && bullet.getY() + bullet.getHeight() > plane.getY()
                        && bullet.getX() + bullet.getWidth() > plane.getX() && bullet.getX() < plane.getX() + plane.getWidth();
                if (flag && plane.getCurrentBlood() <= 1) {
                    //说明撞上  让子弹消失，
                    Ball.balls.remove(bullet);
                    //飞机血量减少
                    plane.setCurrentBlood(plane.getCurrentBlood() - 1);
                    //结束逻辑
                    EndLogic.changePlaneImag(plane);
                    //设置为游戏结束
                    EndLogic.gameOver = true;
                    break;
                } else if (flag && plane.getCurrentBlood() != 1) {
                    //说明撞上  让球消失，
                    Ball.balls.remove(bullet);
                    plane.setCurrentBlood(plane.getCurrentBlood() - 1);
                }
            }
        }
    }


    //判断飞机与bomb的碰撞逻辑
    public static void isEatBomb(Plane plane) {
        boolean flag = false;
        for (int i = 0; i < Bomb.bombs.size(); i++) {
            Bomb bomb = Bomb.bombs.get(i);
            flag = bomb.getY() < plane.getY() + plane.getHeight() && bomb.getY() + bomb.getHeight() > plane.getY()
                    && bomb.getX() + bomb.getWidth() > plane.getX() && bomb.getX() < plane.getX() + plane.getWidth();
            if (flag) {
                System.out.println("1123123");
                Bomb.bombs.remove(bomb);
                Ball.balls.removeAll(Ball.balls);//所有球消失
            }
        }
    }

    //飞机与buff
    public static void isEatBuff(Plane plane) {
        boolean flag = false;
        int x = 0;
        for (int i = 0; i < BulletBuff.bulletBuffs.size(); i++) {
            BulletBuff bulletBuff = BulletBuff.bulletBuffs.get(i);
            flag = bulletBuff.getY() < plane.getY() + plane.getHeight() && bulletBuff.getY() + bulletBuff.getHeight() > plane.getY()
                    && bulletBuff.getX() + bulletBuff.getWidth() > plane.getX() && bulletBuff.getX() < plane.getX() + plane.getWidth();
            if (flag) {
                bulletBuffss.add(bulletBuff);
                BulletBuff.bulletBuffs.remove(bulletBuff);
            }
        }
    }
}
